
// 创建一个SegmentView实例
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var common = require("common");
var http = require("helper/http");
var circleUrl = require("url/circle");
var listdata = mm("do_ListData");
var listview = ui("listView");
listview.bindItems(listdata);

page.on("back", function() {
	app.closePage();
})

/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
ui(addheader+".headerLeft").on("touch", function() {
    app.closePage()
});

//头部标题
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({"labelTitle" : "更多圈子"});
header.refreshData();
/****************处理页面-头部-end*****************/

var noData = [{"template":1,"labelContent":"没有更多圈子!"}];

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	binddata();
});

//成功加入圈子
page.on("join_circle_success", function(data){
	if(data.circleId!=null){
		var joinCircleId = data.circleId;
		for (var i = 0; i < listdata.getCount(); i++) {
			var oneData = listdata.getOne(i);
			if (oneData.circleId==joinCircleId) {
				oneData.canJion = false;
				listdata.updateOne(i, oneData);
				listview.refreshItems();
				break;
			}
		}
	}
});

//当前页码
var pageNum = 1;
//本页面-ajax业务请求url
var httpUrl = circleUrl.url.base.more;//更多圈子

//全部刷新数据
function binddata() {
	pageNum = 1;
	var params = {page : pageNum}
	http.post(httpUrl, params, function(data) {
		if (data.circlePage) {
			var circleList = data.circlePage.content;
			listview.rebound();
			listdata.removeAll();// 清除数据
			if(circleList.length > 0){
				listdata.addData(circleList);
			}else{
				listdata.addData(noData);
			}
			listview.refreshItems();// 刷新item数据
		}
	});
}

function getNextPageData() {
	pageNum++;
	var params = {page : pageNum};
	http.post(httpUrl, params, function(data) {
		if (data.friendPage) {
			var circleList = data.circlePage.content;
			listview.rebound();
			listdata.addData(circleList);
			listview.refreshItems();
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		binddata();
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
		listview.rebound();//复位【一定要做】
	}
});


